<?php
class ModelBlogRoll extends Model {
	public function addRoll($data) {

		$this->db->query("INSERT INTO " . DB_PREFIX . "blog_links SET modified = NOW(), created = NOW()");
		$id = $this->db->getLastId();

		foreach ($data['roll'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "blog_links_description SET 
				blog_link_id = '" . (int)$id . "', 
				language_id = '" . (int)$language_id . "', 
				name = '" . $this->db->escape($value['name']) . "', 
				href = '" . $this->db->escape($value['href']) . "', 
				target = '" . $this->db->escape($value['target']) . "', 
				published = '" . $this->db->escape($value['published']) . "', 
				sort_order = '" . $this->db->escape($value['sort_order']) . "'");
		}

		$this->cache->delete('admin_blog_roll');
	}
	
	public function editRoll($id, $data) {

		$this->db->query("UPDATE " . DB_PREFIX . "blog_links SET modified = NOW() WHERE id = '" . (int)$id . "' LIMIT 1");
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "blog_links_description WHERE blog_link_id = '" . (int)$id . "'");
		foreach ($data['roll'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "blog_links_description SET 
				blog_link_id = '" . (int)$id . "', 
				language_id = '" . (int)$language_id . "', 
				name = '" . $this->db->escape($value['name']) . "', 
				href = '" . $this->db->escape($value['href']) . "', 
				target = '" . $this->db->escape($value['target']) . "', 
				published = '" . $this->db->escape($value['published']) . "', 
				sort_order = '" . $this->db->escape($value['sort_order']) . "'");
		}
		$this->cache->delete('admin_blog_roll');
	}
	
	public function deleteRoll($id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "blog_links WHERE id = '" . (int)$id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "blog_links_description WHERE blog_link_id = '" . (int)$id . "'");
		$this->cache->delete('admin_blog_roll');
	} 

	public function getRoll($id) {
		$query = $this->db->query("SELECT 
										* 
								   FROM 
										" . DB_PREFIX . "blog_links_description as bld 
								   WHERE 
										bld.blog_link_id ='" . (int)$id . "'");
		
		$roll_data = array();
		foreach ($query->rows as $result) {
			$roll_data[$result['language_id']] = array(
				'id'          => $result['blog_link_id'],
				'name'        => $result['name'],
				'href'        => $result['href'],
				'target'      => $result['target'],
				'published'   => $result['published'],
				'sort_order'  => $result['sort_order']
			);
		}	
		
		return $roll_data;
	} 
	
	public function getRolls() {
		
		$sql = "SELECT 
					* 
				FROM 
					" . DB_PREFIX . "blog_links c 
					LEFT JOIN " . DB_PREFIX . "blog_links_description bld ON (c.id = bld.blog_link_id) 
				WHERE 1	";
		
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY c.modified";	
		}
		
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}
	
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}		

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
		
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}	
		
	
		$query = $this->db->query($sql);


		$roll_data = array();
		foreach ($query->rows as $result) {
			$roll_data[$result['blog_link_id']] = array(
				'id'          => $result['id'],
				'name'        => $result['name'],
				'href'        => $result['href'],
				'target'      => $result['target'],
				'published'   => $result['published'],
				'sort_order'  => $result['sort_order']
			);
		}	
	
		return $roll_data;
	}

		
	public function getTotalRolls() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "blog_links");
		
		return $query->row['total'];
	}	
}
?>